package logic
{
	import assets.Globals;
	
	import com.greensock.TweenMax;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import gameobjects.*;
	import gameobjects.enemies.*;
	
	public class CreepManager
	{
		private static var timer:Timer;
		private static var tick:Number = 1000;
		private static var iter:Number = 0;
	
		public static function createCreeps():void
		{
			//putCreep(new Enemy(), 0, 2 * Globals.TILE_HEIGHT);
		}
		
		public static function putCreep(creep:Enemy, x:Number, y:Number):void
		{
			creep.x = x;
			creep.y = y;
			Globals.game.addChild(creep);
		}
		
		private static function timerHandler(event:TimerEvent):void
		{
			iter ++;
			var lane:int = Math.round(Math.random() * 5);
			var x:Number = 760;
			var y:Number = 68 * lane;
			
			if (iter == 5) tick = 3500;
			if (iter == 15) {
				tick = 3000;
				// sleep 4 2nd wave?
			}
			if (iter == 25) tick = 2000;
			if (iter == 35) tick = 1500;
			
			timer.delay = tick;
			
//			var enemy:Enemy = Math.random() > .81 ? new Creep2() : new Creep1();
			
			var enemy:Enemy = new Creep1();
			if (iter > 5 && Math.random() > .60) enemy = new Creep2();
			if (iter > 15 && Math.random() > .80) enemy = new Creep3();
			
			enemy.setFieldY(lane);
			putCreep(enemy, x, y);
		}
		
		/*
		private static function timerCompleteHandler(event:TimerEvent):void
		{
		}
		*/
		
		public static function start():void
		{
			if (null == timer) {
				tick = 4000;
				timer = new Timer(tick, 50);
				timer.addEventListener(TimerEvent.TIMER, timerHandler);
//				timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
			}
			
			timer.start();
		}
		
		public static function pause():void
		{
			timer.stop();
		}
		
		public static function resume():void
		{
			timer.start();
		}
		
		public static function stop():void
		{
			timer.stop();
		}
	}
}